#include "showpic.h"
#include "ui_showpic.h"
#include <QDir>
#include <QDebug>
#include <QResizeEvent>

showpic::showpic(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::showpic)
{
    ui->setupUi(this);
    ui->pushButton->setStyleSheet("border-image: url(./btn1.jpg);");
    ui->pushButton_2->setStyleSheet("border-image: url(./btn1.jpg);");
    ui->pushButton_3->setStyleSheet("border-image: url(./btn1.jpg);");

    QPalette pal = this->palette();
    pal.setBrush(QPalette::Background,QBrush(QPixmap("./bg.jpg")));
    setPalette(pal);
    // 设置标签属性
    ui->label->setAlignment(Qt::AlignCenter);
    ui->label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    ui->label->setMinimumSize(200, 150); // 最小尺寸保证按钮可见

    // 确保按钮在最上层
    ui->pushButton->raise();
    ui->pushButton_2->raise();
    ui->pushButton_3->raise();

    loadImages();
    showCurrentImage();
}

showpic::~showpic()
{
    delete ui;
}

void showpic::on_pushButton_clicked()
{
    if (QWidget *camera = this->parentWidget()) {
        camera->show();
        camera->raise();
    }
    this->close();
}

void showpic::on_pushButton_2_clicked()
{
    if (imageList.isEmpty()) return;

    if (currentIndex > 0) currentIndex--;
    else currentIndex = imageList.size() - 1;
    showCurrentImage();
}

void showpic::on_pushButton_3_clicked()
{
    if (imageList.isEmpty()) return;

    currentIndex = (currentIndex + 1) % imageList.size();
    showCurrentImage();
}

void showpic::loadImages()
{
    QDir dir("./captured");
    dir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
    dir.setNameFilters(QStringList() << "*.jpg" << "*.jpeg" << "*.png" << "*.bmp");
    imageList = dir.entryInfoList();

    qDebug() << "找到图片: " << imageList.size() << "张";
}

void showpic::showCurrentImage()
{
    if (imageList.isEmpty()) {
        ui->label->setText("没有图片");
        return;
    }

    if (currentIndex >= 0 && currentIndex < imageList.size()) {
        QString imagePath = imageList[currentIndex].filePath();
        QPixmap pix;

        if (!pix.load(imagePath)) {
            ui->label->setText("加载图片失败: " + imagePath);
            return;
        }

        // 获取标签的可用尺寸
        QSize labelSize = ui->label->size();

        // 计算最大允许尺寸 - 确保按钮可见
        int maxWidth = labelSize.width() - 50; // 留出按钮空间
        int maxHeight = labelSize.height() - 50;

        // 如果图片太大，进行缩放
        if (pix.width() > maxWidth || pix.height() > maxHeight) {
            pix = pix.scaled(maxWidth, maxHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
        }

        ui->label->setPixmap(pix);
        this->setWindowTitle(QString("图片 %1/%2").arg(currentIndex+1).arg(imageList.size()));

        // 确保按钮在最上层
        ui->pushButton->raise();
        ui->pushButton_2->raise();
        ui->pushButton_3->raise();
    }
}

// 添加窗口大小调整事件处理
void showpic::resizeEvent(QResizeEvent *event)
{
    QMainWindow::resizeEvent(event);
    // 窗口大小变化时重新显示图片
    showCurrentImage();
}
